package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Candidateresume;
import org.example.entity.Employee;
import org.example.entity.Offermanagementtable;
import org.example.entity.Recruitmentrequirement;
import org.example.models.R;
import org.example.service.CandidateresumeService;
import org.example.service.EmployeeService;
import org.example.service.OffermanagementtableService;
import org.example.service.RecruitmentrequirementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/offermanagementtable")
public class OffermanagementtableServiceController {
    @Autowired
    private OffermanagementtableService offermanagementtableService;
    @Autowired
    private CandidateresumeService candidateresumeService;
    @Autowired
    private RecruitmentrequirementService recruitmentrequirementService;
    @Autowired
    private EmployeeService employeeService;

    //根据off状态查询所有
    @GetMapping("/findByOffStatus")
    public R<Page<Offermanagementtable>> findByOffStatus(
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition,
            @RequestParam(value = "deptName", required = false) String deptName) {


        List<Offermanagementtable> byStatus = offermanagementtableService.findByStatus(status, intendedPosition, deptName);
        int start = (current - 1) * size;
        int end = Math.min(start + size, byStatus.size());
        List<Offermanagementtable> subList = byStatus.subList(start, end);
        Page<Offermanagementtable> page = new Page<>(current, size, byStatus.size()); //
        page.setRecords(subList);
        System.out.println(byStatus);
        return R.success(page);

    }

    //升序
    @GetMapping("/sortByTime")
    public R<Page<Offermanagementtable>> findByStatusAsc(
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition,
            @RequestParam(value = "deptName", required = false) String deptName) { //默认为已发放


        List<Offermanagementtable> byStatus = offermanagementtableService.findByStatusAsc(status, intendedPosition, deptName);
        int start = (current - 1) * size;
        int end = Math.min(start + size, byStatus.size());
        List<Offermanagementtable> subList = byStatus.subList(start, end);
        Page<Offermanagementtable> page = new Page<>(current, size, byStatus.size()); //
        page.setRecords(subList);
        System.out.println(byStatus);
        return R.success(page);

    }

    //降序
    @GetMapping("/sortByTimeDesc")
    public R<Page<Offermanagementtable>> findByStatusDesc(
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition,
            @RequestParam(value = "deptName", required = false) String deptName) { //默认为已发放


        List<Offermanagementtable> byStatus = offermanagementtableService.findByStatusDesc(status, intendedPosition, deptName);
        int start = (current - 1) * size;
        int end = Math.min(start + size, byStatus.size());
        List<Offermanagementtable> subList = byStatus.subList(start, end);
        Page<Offermanagementtable> page = new Page<>(current, size, byStatus.size()); //
        page.setRecords(subList);
        System.out.println(byStatus);
        return R.success(page);

    }

    //offer跟进
    // 查询所有（待发offer除外）
    @GetMapping("/findAllStatus")
    public R<Page<Offermanagementtable>> findAllStatus(
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition,
            @RequestParam(value = "deptName", required = false) String deptName) { //默认为已发放
        status = "待发offer";

        List<Offermanagementtable> byStatus = offermanagementtableService.findAllStatus(status, intendedPosition, deptName);
        int start = (current - 1) * size;
        int end = Math.min(start + size, byStatus.size());
        List<Offermanagementtable> subList = byStatus.subList(start, end);
        Page<Offermanagementtable> page = new Page<>(current, size, byStatus.size()); //
        page.setRecords(subList);
        System.out.println(byStatus);
        return R.success(page);
    }

    //升序
    @GetMapping("/moreAdvancedStatusAscendingOrder")
    public R<Page<Offermanagementtable>> moreAdvancedStatusAscendingOrder(
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition,
            @RequestParam(value = "deptName", required = false) String deptName) {
        List<Offermanagementtable> byStatus;
        if ("已拒绝".equals(status) || "已入职".equals(status) || "待入职".equals(status)) {
            byStatus = offermanagementtableService.findByStatusAsc(status, intendedPosition, deptName);

        } else
            byStatus = offermanagementtableService.moreAdvancedStatusAscendingOrder(status, intendedPosition, deptName);
        int start = (current - 1) * size;
        int end = Math.min(start + size, byStatus.size());
        List<Offermanagementtable> subList = byStatus.subList(start, end);
        Page<Offermanagementtable> page = new Page<>(current, size, byStatus.size()); //
        page.setRecords(subList);
        System.out.println(byStatus);
        return R.success(page);
    }

    //降序
    @GetMapping("/followUpOnTheDescendingOrderOfStatus")
    public R<Page<Offermanagementtable>> followUpOnTheDescendingOrderOfStatus(
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "intendedPosition", required = false) String intendedPosition,
            @RequestParam(value = "deptName", required = false) String deptName) {
        List<Offermanagementtable> byStatus;
        if ("已拒绝".equals(status) || "已入职".equals(status) || "待入职".equals(status)) {
            byStatus = offermanagementtableService.findByStatusDesc(status, intendedPosition, deptName);
        } else
            byStatus = offermanagementtableService.followUpOnTheDescendingOrderOfStatus(status, intendedPosition, deptName);
        int start = (current - 1) * size;
        int end = Math.min(start + size, byStatus.size());
        List<Offermanagementtable> subList = byStatus.subList(start, end);
        Page<Offermanagementtable> page = new Page<>(current, size, byStatus.size());
        page.setRecords(subList);
        System.out.println(byStatus);
        return R.success(page);
    }

    // 添加
    @PostMapping("/insert")
    public R<String> insert(@RequestBody Offermanagementtable offermanagementtable,
    @RequestAttribute("userId") Integer userId) {
        Employee byId = employeeService.getById(userId);
        offermanagementtable.setSenderBy(byId.getEmployeeName());
        boolean save = offermanagementtableService.save(offermanagementtable);
        if (save) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    //通过简历id查询用户
    @GetMapping("/finfindByResumeId")
    public R<Offermanagementtable> finfindByResumeId(@RequestParam("applicanId") String applicanId) {
        Offermanagementtable byResumeId = offermanagementtableService.findByResumeId(applicanId);
        return R.success(byResumeId);
    }

    //根据id进行修改
    @PutMapping("/update")
    public R update(@RequestBody Offermanagementtable offermanagementtable) {

        boolean update = offermanagementtableService.updateById(offermanagementtable);
        if (update) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    // 通过简历ID查询Offer状态，并关联候选人简历
    @GetMapping("/OfferWithCandidateResultMap")
    public R<List<Offermanagementtable>> OfferWithCandidateResultMap(@RequestParam(value = "candidateId", required = false) String candidateId) {
        List<Offermanagementtable> offerWithCandidateResultMap = offermanagementtableService.OfferWithCandidateResultMap(candidateId);
        return R.success(offerWithCandidateResultMap);
    }

    // 通过OfferID查询Offer状态，并关联候选人简历和部门
    @GetMapping("/getOfferWithCandidateAndDept")
    public R<List<Offermanagementtable>> getOfferWithCandidateAndDept(@RequestParam(value = "offerId", required = false) String offerId) {
        List<Offermanagementtable> offerWithCandidateAndDept = offermanagementtableService.getOfferWithCandidateAndDept(offerId);
        return R.success(offerWithCandidateAndDept);
    }

    // 通过OfferID修改Offer状态
    @PutMapping("/updateOfferStatus")
    public R updateOfferStatus(@RequestBody Offermanagementtable offermanagementtable) {

        boolean update = offermanagementtableService.updateById(offermanagementtable);
        Offermanagementtable byId1 = offermanagementtableService.getById(offermanagementtable.getOfferid());
        Candidateresume candidateresume = new Candidateresume();
        candidateresume.setCandidateid(Integer.valueOf(byId1.getApplicantId()));
        candidateresume.setResumeStatus("已入职");
        candidateresumeService.updateById(candidateresume);
        if (update) {
            Recruitmentrequirement byId = recruitmentrequirementService.getById(byId1.getTheLocationNumber());
            if (byId != null) {
                byId.setNumberofpositions(byId.getNumberofpositions() - 1);
                recruitmentrequirementService.updateById(byId);
            } else if (byId.getNumberofpositions() >= 0) {
                Recruitmentrequirement recruitmentrequirement = new Recruitmentrequirement();
                recruitmentrequirement.setRequirementid(Integer.valueOf(offermanagementtable.getTheLocationNumber()));
                recruitmentrequirement.setStatus("已完成");
                boolean update1 = recruitmentrequirementService.updateById(recruitmentrequirement);
            }
            return R.success(200,"修改成功");
        } else {
            return R.error(400, "修改失败");
        }
    }
    // 通过OfferID查询Offer状态并读取简历信息
    @GetMapping("/offerstatus")
    public  R<List<Offermanagementtable>> getOfferByStatus(@RequestParam String status){
        status = "已入职";
        List<Offermanagementtable> offerByStatus = offermanagementtableService.selectByOfferStatus(status);
        return R.success(offerByStatus);
    }
}
